import { apiQueryFinallyInfo } from 'js/api'
import { session } from 'js/util'

export default {
  state: {
    helpMenus: [],
    helpActionMenuIndex: 0
  },
  getters: {
    helpMenus: state => state.helpMenus,
    helpActionMenuIndex: state => state.helpActionMenuIndex
  },
  actions: {
    // 获取帮助中心列表
    fetchQueryFinallyInfo({ state }, payload) {
      const sessionHelpMenus = session.get('helpMenus') || []
      if (sessionHelpMenus.length) {
        state.helpMenus = sessionHelpMenus
        return
      }
      let result
      apiQueryFinallyInfo().then(data => {
        if (data.data) {
          data = data.data
        }
        state.helpMenus = []
        result = data.finallyInfoList || []
        // result = result.splice(0, 5)
        result.forEach(info => {
          if (info.articleList.length) {
            let obj = {title: info.className, isShow: false, hasChild: !!info.articleList.length}
            obj.childs = info.articleList.map(item => {
              return {
                title: item.title,
                date: item.date,
                content: item.content,
                link: '/helpCenter/' + item.title
              }
            })
            state.helpMenus.push(obj)
          }
        })
        session.set('helpMenus', state.helpMenus)
        payload.callback && payload.callback()
      })
    }
  },
  mutations: {
    setActionMenuIndex(state, index) {
      state.helpActionMenuIndex = index
    }
  }
}
